import Promise from "./promise-4.js";

// 问题1 x === promise2的问题
/*
let promise2 = new Promise((resolve, reject) => {
  resolve("ok");
}).then(() => {
  return promise2; // then完之后会返回一个新的promise，但是我们没有调用他的resolve和reject
});

promise2.then(
  (value) => {
    console.log("成功", value);
  },
  (reason) => {
    console.log("成功", reason);
  }
);
*/

// 问题2: 取then有异常的问题
// let promise = {};
// let times = 0;
// Object.defineProperty(promise, "then", {
//   get() {
//     if (++times == 2) {
//       throw new Error("出错");
//     }
//   },
// });
// promise.then;
// promise.then;

const otherPromise = {
  then(onFulfilled, onRejected) {
    onFulfilled(123);
    onFulfilled(123);
    onRejected(123);
    throw new Error("error");
  },
};

let promise2 = new Promise((resolve, reject) => {
  resolve("ok");
}).then(() => {
  return otherPromise;
  //   new Promise((resolve, reject) => {
  //     setTimeout(() => {
  //       resolve("ok");
  //     }, 1000);
  //   });
});

promise2.then(
  (value) => {
    console.log("成功", value);
  },
  (reason) => {
    console.log("失败", reason);
  }
);
